<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="" prop="internalOrderNumber">
        <el-input
          v-model="queryParams.internalOrderNumber"
          placeholder="请输入内部订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="orderProperties">
        <el-input
          v-model="queryParams.orderProperties"
          placeholder="请输入订单属性"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付日期" prop="dataDate">
        <!-- <el-date-picker
          clearable
          v-model="queryParams.dataDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="支付日期"
        >
        </el-date-picker> -->
        <el-date-picker
          v-model="value2"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          @change="changeTime"
          size="mini"
        >
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="" prop="storeSite">
        <el-input
          v-model="queryParams.storeSite"
          placeholder="请输入商店站点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入店铺"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item label="" prop="buyerId">
        <el-input
          v-model="queryParams.buyerId"
          placeholder="请输入买家ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入省"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="validNum">
        <el-input
          v-model="queryParams.validNum"
          placeholder="请输入有效件数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="payDay">
        <el-date-picker
          clearable
          v-model="queryParams.payDay"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择支付日期-天"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="" prop="payDateYear">
        <el-input
          v-model="queryParams.payDateYear"
          placeholder="请输入支付日期-年"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="payDateMonth">
        <el-input
          v-model="queryParams.payDateMonth"
          placeholder="请输入支付日期-月"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="" prop="payDateDay">
        <el-input
          v-model="queryParams.payDateDay"
          placeholder="请输入支付日期-日"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="" prop="payDateWeek">
        <el-input
          v-model="queryParams.payDateWeek"
          placeholder="请输入业务日期-周"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="逻辑删除;0=未删除；1=已删除" prop="isDelete">
        <el-input v-model="queryParams.isDelete" placeholder="请输入逻辑删除;0=未删除；1=已删除" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-dialog
      title="选择时间"
      :visible.sync="openInfo"
      width="700px"
      append-to-body
      v-if="openInfo"
    >
      <timeSearch
        @getDateArr="getDateArr"
        @changDay="changDay"
        :timeArr="timeArr"
        :dayActive="this.dayActive"
        :dayLimit="1095"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="handleRefreshCache">
          同步数据
        </el-button>
      </div>
    </el-dialog>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" style="display: none">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['product:BiOrder:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5" style="display: none">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:BiOrder:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5" style="display: none">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:BiOrder:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['product:BiOrder:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="openInfo = true"
          >同步数据</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <div ref="tableDom">
      <el-table
        v-loading="loading"
        :maxHeight="BiOrderList.length ? tableHeight : 'auto'"
        :data="BiOrderList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="ID" align="center" prop="id" />
        <el-table-column
          width="140"
          label="原始线上订单号"
          align="center"
          prop="origOnlineOrderNumber"
        />
        <el-table-column
          label="内部订单号"
          align="center"
          prop="internalOrderNumber"
        />
        <el-table-column
          label="订单属性"
          align="center"
          prop="orderProperties"
        />
        <el-table-column label="品牌" align="center" prop="brand" />
        <el-table-column label="商店站点" align="center" prop="storeSite" />
        <el-table-column label="店铺" align="center" prop="shopName" />
        <!-- <el-table-column label="支付日期" align="center" prop="payDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.payDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column> -->
        <el-table-column label="买家ID" align="center" prop="buyerId" />
        <el-table-column label="省" align="center" prop="province" />
        <el-table-column label="市" align="center" prop="city" />
        <el-table-column
          width="100"
          label="销售总金额 "
          align="center"
          prop="salesAmountAll"
        />
        <el-table-column
          width="100"
          label="总销售件数"
          align="center"
          prop="validNumAll"
        />
        <el-table-column
          width="100"
          label="产品销售件数"
          align="center"
          prop="validNum"
        />
        <el-table-column
          width="100"
          label="产品销售金额"
          align="center"
          prop="salesAmount"
        />
        <el-table-column
          label="支付日期-天"
          align="center"
          prop="payDay"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.payDay, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="支付日期-年"
          align="center"
          prop="payDateYear"
        />
        <el-table-column
          label="支付日期-月"
          align="center"
          prop="payDateMonth"
        />
        <el-table-column label="支付日期-日" align="center" prop="payDateDay" />
        <el-table-column
          label="业务日期-周"
          align="center"
          prop="payDateWeek"
        />
        <!-- <el-table-column label="逻辑删除;0=未删除；1=已删除" align="center" prop="isDelete" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['product:BiOrder:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['product:BiOrder:remove']">删除</el-button>
        </template>
      </el-table-column> -->
      </el-table>
    </div>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改t3订单明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="内部订单号" prop="internalOrderNumber">
          <el-input
            v-model="form.internalOrderNumber"
            placeholder="请输入内部订单号"
          />
        </el-form-item>
        <el-form-item label="订单属性" prop="orderProperties">
          <el-input
            v-model="form.orderProperties"
            placeholder="请输入订单属性"
          />
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="商店站点" prop="storeSite">
          <el-input v-model="form.storeSite" placeholder="请输入商店站点" />
        </el-form-item>
        <el-form-item label="店铺" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入店铺" />
        </el-form-item>
        <el-form-item label="支付日期" prop="payDate">
          <el-date-picker
            clearable
            v-model="form.payDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择支付日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="买家ID" prop="buyerId">
          <el-input v-model="form.buyerId" placeholder="请输入买家ID" />
        </el-form-item>
        <el-form-item label="省" prop="province">
          <el-input v-model="form.province" placeholder="请输入省" />
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-input v-model="form.city" placeholder="请输入市" />
        </el-form-item>
        <el-form-item label="有效件数" prop="validNum">
          <el-input v-model="form.validNum" placeholder="请输入有效件数" />
        </el-form-item>
        <el-form-item label="支付日期-天" prop="payDay">
          <el-date-picker
            clearable
            v-model="form.payDay"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择支付日期-天"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付日期-年" prop="payDateYear">
          <el-input
            v-model="form.payDateYear"
            placeholder="请输入支付日期-年"
          />
        </el-form-item>
        <el-form-item label="支付日期-月" prop="payDateMonth">
          <el-input
            v-model="form.payDateMonth"
            placeholder="请输入支付日期-月"
          />
        </el-form-item>
        <!-- <el-form-item label="支付日期-日" prop="payDateDay">
          <el-input v-model="form.payDateDay" placeholder="请输入支付日期-日" />
        </el-form-item> -->
        <el-form-item label="业务日期-周" prop="payDateWeek">
          <el-input
            v-model="form.payDateWeek"
            placeholder="请输入业务日期-周"
          />
        </el-form-item>
        <!-- <el-form-item label="逻辑删除;0=未删除；1=已删除" prop="isDelete">
          <el-input v-model="form.isDelete" placeholder="请输入逻辑删除;0=未删除；1=已删除" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import timeSearch from "./day.vue";
import {
  listBiOrder,
  getBiOrder,
  delBiOrder,
  addBiOrder,
  updateBiOrder,
  refreshCache,
} from "@/api/chart/BiOrder";
import moment from "moment";
export default {
  name: "BiOrder",
  dicts: ["sys_product_live"],
  components: { timeSearch },
  data() {
    const end = new Date();
    const start = new Date();
    start.setTime(start.getTime() - 30 * 24 * 60 * 60 * 1000); // 30天前
    return {
      openInfo: false,
      dayActive: 2,
      timeArr: [
        { value: "2", label: "昨天" },
        { value: "7", label: "最近7天" },
        { value: "30", label: "最近30天" },
        { value: "365", label: "最近一年" },
        { value: "1", label: "日" },
      ],
      timeGap: { startTime: "", endTime: "" },
      //纵向滚动条
      tableHeight: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // t3订单明细表格数据
      BiOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        internalOrderNumber: null,
        orderProperties: null,
        brand: null,
        storeSite: null,
        shopName: null,
        payDate: null,
        buyerId: null,
        province: null,
        city: null,
        validNum: null,
        payDay: null,
        payDateYear: null,
        payDateMonth: null,
        payDateDay: null,
        payDateWeek: null,
        isDelete: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        internalOrderNumber: [
          { required: true, message: "内部订单号不能为空", trigger: "blur" },
        ],
      },
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      value1: "",
      value2: [start, end], // 默认选中最近30天
    };
  },
  created() {
    this.changeTime();
  },
  mounted() {
    var _this = this;
    this.resizeTable();
    window.onresize = function () {
      _this.resizeTable();
    };
  },
  //记得清空  如果在keepalive中 则使用activated  deactivated
  destroyed() {
    window.onresize = null;
  },
  methods: {
    changeTime() {
      // console.log(JSON.stringify(this.value2));
      let m1 = moment(this.value2[0]).format("YYYY-MM-DD");
      let m2 = moment(this.value2[1]).format("YYYY-MM-DD");
      // console.log(JSON.stringify(m1));
      // console.log(JSON.stringify(m2));
      this.queryParams.payDayStart = m1;
      this.queryParams.payDayEnd = m2;
      this.getList();
      // this.queryParams.startDate = m1;
      // this.queryParams.endDate = m2;
    },
    // 打开加载层
    openLoading() {
      this.$modal.loading("正在加载数据，请稍候！");
    },
    /** 刷新缓存按钮操作 Warning*/
    handleRefreshCache() {
      // this.$modal.msgWarning("数据正在同步，请稍候");
      const obj = {
        startDate: this.timeGap.startTime,
        endDate: this.timeGap.endTime,
      };
      this.openLoading();
      refreshCache(obj).then(() => {
        this.$modal.closeLoading();
        this.openInfo = false;
        this.$modal.msgSuccess("同步数据成功");
        this.getList();
        // this.$store.dispatch('dict/cleanDict');
      });
    },
    //时间控件
    changDay(val) {
      this.dayActive = val;
      this.timeGap.startTime = val[0];
      this.timeGap.endTime = val[1];
    },
    getDateArr(value) {
      this.timeGap.startTime = value[0];
      this.timeGap.endTime = value[1];
      console.log(JSON.stringify(this.timeGap));
      this.dayActive = 1;
    },
    // table自动高度
    returnTableHeight(ref, bottomOffset) {
      let height = null;
      height =
        window.innerHeight - ref.getBoundingClientRect().top - bottomOffset;
      return height;
    },

    resizeTable() {
      this.$nextTick(() => {
        this.tableHeight = this.returnTableHeight(this.$refs.tableDom, 100);
      }, 0);
    },
    /** 查询t3订单明细列表 */
    getList() {
      this.loading = true;
      listBiOrder(this.queryParams).then((response) => {
        this.BiOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        internalOrderNumber: null,
        orderProperties: null,
        brand: null,
        storeSite: null,
        shopName: null,
        payDate: null,
        buyerId: null,
        province: null,
        city: null,
        validNum: null,
        payDay: null,
        payDateYear: null,
        payDateMonth: null,
        payDateDay: null,
        payDateWeek: null,
        isDelete: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {};
      this.value2 = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加t3订单明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBiOrder(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改t3订单明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBiOrder(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBiOrder(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除t3订单明细编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBiOrder(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "product/BiOrder/export",
        {
          ...this.queryParams,
        },
        `BiOrder_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
